package com.ksv.qlsv;

import com.ksv.lop.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HocKyEditActivity extends Activity {
	private TextView txt_id;
	private TextView txt_name;
	private Button btn_save;
	private Button btn_cancel;
	
	public static HocKy hk = new HocKy();
	public static int iAddEditView = 1;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogedit);            
        this.setTitle(getResources().getString(R.string.monhoc));
        btn_save = (Button)findViewById(R.id.btn_save);
        btn_save.setText(getResources().getString(R.string.luu));
		btn_cancel = (Button)findViewById(R.id.btn_cancel);
		btn_cancel.setText(getResources().getString(R.string.huy));
        txt_id = (TextView)findViewById(R.id.txt_id);
        txt_name = (TextView)findViewById(R.id.txt_name);    
        if(iAddEditView == 1){
        	hk.setMaHocKy(ConnectDB.getHocKyMax());
        	txt_id.setText(String.valueOf(hk.getMaHocKy()));
        	txt_id.setEnabled(false);
        	txt_name.setText("");
        	txt_name.setEnabled(true);
        }
        else if(iAddEditView == 2){
        	txt_id.setText(String.valueOf(hk.getMaHocKy()));        
        	txt_id.setEnabled(false);
        	txt_name.setText(hk.getTenHocKy());
        }
        else{
        	txt_id.setText(String.valueOf(hk.getMaHocKy()));        
        	txt_id.setEnabled(false);
        	txt_id.setEnabled(false);        	
        	txt_name.setEnabled(false);
        	btn_save.setEnabled(false);
        	txt_name.setText(hk.getTenHocKy());
        }
        onClickSave();
        onClickCancel();        
    }

	private void onClickSave() {
		// TODO Auto-generated method stub		
		btn_save.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(iAddEditView == 1){
					hk.setTenHocKy(txt_name.getText().toString());
					ConnectDB.AddHocKy(hk);
					HocKyEditActivity.this.finish();
				}
				else if(iAddEditView == 2){
					hk.setTenHocKy(txt_name.getText().toString());
					ConnectDB.updateHocKy(hk);
					HocKyEditActivity.this.finish();
				}
				else{
					HocKyEditActivity.this.finish();
				}
			}
		});
	}

	private void onClickCancel() {
		// TODO Auto-generated method stub
		btn_cancel.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub				
				HocKyEditActivity.this.finish();
			}
		});		
	}
	
	
}
